package com.example.liuke.guangguang.ui;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.ImageView;

import com.example.liuke.guangguang.R;

public class IMEActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView iv_alpha;
    private AlphaAnimation alphaAnimation;
    private TransitionDrawable transitionDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ime);

        Button btn_play1 = (Button) findViewById(R.id.btn_play1);
        Button btn_play2 = (Button) findViewById(R.id.btn_play2);
        btn_play1.setOnClickListener(this);
        btn_play2.setOnClickListener(this);
        iv_alpha = (ImageView) findViewById(R.id.iv_alpha);
        //一开始先设置透明，这样图片不会显示，等点击按钮时再显示
        iv_alpha.setAlpha(0.0f);
        alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
        alphaAnimation.setDuration(3000);    //深浅动画持续时间
        alphaAnimation.setFillAfter(true);   //动画结束时保持结束的画面
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_play1) {
            iv_alpha.setImageResource(R.drawable.bookshelf_layer_center);
            iv_alpha.setAlpha(1.0f);
            iv_alpha.setAnimation(alphaAnimation);
            alphaAnimation.start();
        } else if (v.getId() == R.id.btn_play2) {
            //淡入淡出动画需要先设置一个Drawable数组，用于变换图片
            Drawable[] drawableArray = {
                    getResources().getDrawable(R.drawable.bookshelf_layer_center),
                    getResources().getDrawable(R.drawable.back_card_3)
            };
            transitionDrawable = new TransitionDrawable(drawableArray);
            iv_alpha.setImageDrawable(transitionDrawable);
            transitionDrawable.startTransition(3000);
        }

    }

}
